package com.njupt.controller;

import com.njupt.common.pojo.EasyUIDataGridResult;
import com.njupt.common.pojo.JsonUtils;
import com.njupt.common.pojo.TaotaoResult;
import com.njupt.pojo.TbItem;
import com.njupt.pojo.TbItemParam;
import com.njupt.service.ItemService;
import com.njupt.util.Constant;
import com.njupt.util.UploadResp;
import com.njupt.util.UploadUtil;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.qiniu.common.Zone;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: XJL
 * @Description:商品管理Controller
 * @Date: Create in 16:13 2018/5/24
 * @Modified By:
 **/

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Value("${ACCESS_KEY}")
    private String ACCESS_KEY;
    @Value("${SECRET_KEY}")
    private String SECRET_KEY;

    String bucketname = "taotao";


    /**
     * 根据商品id查商品
     * @param itemId
     * @return
     */
    @RequestMapping("/item/{itemId}")
    @ResponseBody
    public TbItem getItemBy(@PathVariable Long itemId){
        return itemService.getItemById(itemId);
    }

    /**
     * cms商品列表分页展示
     * @param page
     * @param rows
     * @return
     */
    @ResponseBody
    @RequestMapping("/item/list")
    public EasyUIDataGridResult getItemList(Integer page,Integer rows){
        EasyUIDataGridResult result = itemService.getItemList(page,rows);
        return result;
    }

    /**
     * cms商品新增
     * @param item
     * @param desc
     * @return
     */
    @ResponseBody
    @RequestMapping("/item/save")
    public TaotaoResult addItem(TbItem item,String desc){
        System.out.println(item);
        TaotaoResult result = itemService.addItem(item,desc);
        return result;
    }

    /**
     * 图片上传
     * @param uploadFile
     * @return
     */
    @ResponseBody
    @RequestMapping("/pic/upload")
    public String picUpload(MultipartFile  uploadFile) throws IOException {
        UploadResp resp = new UploadResp();
        try {
            String picName = UploadUtil.Uploadpic(uploadFile);
            resp.setUrl(Constant.url+picName);
            resp.setError(0);
            resp.setMesssage("上传成功！");
        }catch (Exception e){

            resp.setError(1);
            resp.setMesssage("上传失败！");
        }


        return JsonUtils.objectToJson(resp);
    }
    /**
     * cms商品删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/rest/item/delete")
    public TaotaoResult deleteItem(@RequestParam("ids") Long ids){
        System.out.println("++++++"+ids);
        TaotaoResult result = itemService.deleteItem(ids);
        return result;
    }

    /**
     * 根据商品Id下架商品
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/rest/item/instock")
    public TaotaoResult outshellItem(Long ids){
        TaotaoResult result = itemService.unshellItem(ids);
        return result;
    }

    /**
     * 根据商品Id下架商品
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/rest/item/reshelf")
    public TaotaoResult onshellItem(Long ids){
        TaotaoResult result = itemService.unshellItem(ids);
        return result;
    }

    @ResponseBody
    @RequestMapping("/rest/item/update")
    public TaotaoResult updateItem(TbItem item,String itemDesc,TbItemParam tbItemParam){
        TaotaoResult result = itemService.updateItem(item,itemDesc,tbItemParam);
        return result;
    }
}
